package com.cn.medicalcare.controller.backstageservlet.home;

import com.cn.medicalcare.customtool.JsonTool;
import com.cn.medicalcare.pojo.Advertisement;
import com.cn.medicalcare.pojo.Navigation;
import com.cn.medicalcare.service.AdvertisementService;
import com.cn.medicalcare.service.NavigationService;
import com.cn.medicalcare.service.serviceimpl.AdvertisementServiceImpl;
import com.cn.medicalcare.service.serviceimpl.NavigationServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedHashMap;
import java.util.Map;

@WebServlet("/backstage/home/updateadvertisement.json")
public class AdvertisementUpdateServlet extends HttpServlet {

    private AdvertisementService advertisementService = AdvertisementServiceImpl.getInstance();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        String adnum = req.getParameter("adnum");
        String img = req.getParameter("img");
        String content = req.getParameter("content");
        String survival = req.getParameter("survival");
        Advertisement advertisement = new Advertisement();
        Map<String, Object> map = new LinkedHashMap<>();
        PrintWriter pw = resp.getWriter();
        try {
            advertisement.setId(Integer.valueOf(id));
            advertisement.setAdnum(Integer.valueOf(adnum));
            advertisement.setSurvival(Integer.valueOf(survival));
            advertisement.setImg(img).setContent(content);
            if (advertisementService.updateAdvertisement(advertisement)) {
                map.put("status", 200);
            } else {
                map.put("status", 500);
            }
        } catch (Exception e) {
            map.put("status", 400);
        }
        pw.print(JsonTool.toJson(map));
    }
}
